home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
- Subject: Re: C coding problem
- Date: 14 Apr 1996 10:53:37 -0500
- Organization: none
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4kr721$k7i@solutions.solon.com>
- References: <4ianbf$h86@solutions.solon.com> <4iemcl$a05@solutions.solon.com> <4io1io$no4@solutions.solon.com> <4j06na$808@solutions.solon.com> <4jttan$3gf@solutions.solon.com> <4kg8gl$iht@solutions.solon.com>
- Reply-To: fred@genesis.demon.co.uk
- NNTP-Posting-Host: solutions.solon.com
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4kg8gl$iht@solutions.solon.com> mshan@ibm.net "Mike Shannon" writes:
-
- >>Secondly, why not use arrays? In many places they are easier to
- >>understand - I for one am not going to compromise the readability
- >>of my programs because of some theoretical bull about them
- >>being second rate objects. (whatever that is intended to mean)
- >>a[i] is converted to *(a + i) by the compiler in any case so
- >>whats the advantage with your approach?
- >>
- >>Niall
- >
- >Sure, a[i] is the same as *(a+i). Both involve keeping up with
- >two variables, and, when i incremented, require a multiplication
- >of i times sizeof(*a) to add to a to find the address of the next
- >row.
-
- Conceptually that's true. However it is rather unlikely that a current
- compiler would generate code that looks like that unless it is efficient.
- Strength reduction i.e. converting the loop to one based on pointers is
- a very standard optimisation these days. Where there is a difference I
- would expect the array based version to generate the more efficient code
- more often than not since you are giving the optimiser more information
- to work with. The key here is that a (the pointer) be invariant in the
- loop.
-
- >But the example above is doing neither. p++ involves only one
- >variable, and only addition of sizeof(*p) to find the address of
- >the next row. Notice especially that there is no multiplication
- >required. Agreed the per event difference is small, but if a
- >program does a large number of array sweeps, the small
- >advantage adds up. Some optimizers may do this for you.
-
- A compiler that doesn't is way behind the current state of play.
-
- >I agree with simple syntax, though, and expect that something
- >like:
- >
- > while (p < end_p)
- > {
- > ++(*p);
-
- What's wrong with ++*p?
-
- > p++;
- > }
- >
- >would produce the same assembly output as the above example,
- >and pose less interpretation risk in the future.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-